package com.wwj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wwj.common.constant.MessageConstant;
import com.wwj.common.context.BaseContext;
import com.wwj.common.result.PageResult;
import com.wwj.mapper.OrderMapper;
import com.wwj.mapper.ReportMapper;
import com.wwj.pojo.dto.ReportDTO;
import com.wwj.pojo.dto.ReportPageQueryDTO;
import com.wwj.pojo.entity.Check;
import com.wwj.pojo.entity.Order;
import com.wwj.pojo.entity.Report;
import com.wwj.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.beans.beancontext.BeanContext;
import java.util.List;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Resource
    private ReportMapper reportMapper;

    @Resource
    private OrderMapper orderMapper;

    //生成报告
    @Override
    public void insertReport(ReportDTO reportDTO) {
        Report report=new Report();
        BeanUtils.copyProperties(reportDTO,report);

        Order order = orderMapper.selectById(reportDTO.getOrderId());
        if(order.getReportStatus() == Order.REPORT_STATUS2 ){
            throw new RuntimeException(MessageConstant.REPORT_STATUS2);
        }
        if(order.getStatus() == Order.STATUS1 || order.getStatus() == Order.STATUS3){
            throw new RuntimeException(MessageConstant.STATUS_1_3_RESPORTSTATUS);
        }
        report.setNumber(String.valueOf(System.currentTimeMillis()));
        report.setStatus(Report.REPORT_STATUS2);
        report.setUserId(order.getUserId());
        order.setReportStatus(Order.REPORT_STATUS2);
        orderMapper.update(order);
        log.info("report{}",report);
        reportMapper.insertReport(report);
    }


    //删除体检报告
    @Override
    public void deleteReport(Long id) {

        reportMapper.deleteById(id);
    }


    //修改体检报告
    @Override
    public void update(ReportDTO reportDTO) {
        Report report=Report.builder()
                .id(reportDTO.getId())
                .reportDescription(reportDTO.getReportDescription())
                .build();
        reportMapper.updateDescription(report);
    }

    //体检报告分页查询-管理端
    @Override
    public PageResult pageQuery(ReportPageQueryDTO reportPageQueryDTO) {
        PageHelper.startPage(reportPageQueryDTO.getPage(),reportPageQueryDTO.getPageSize());
        Page<Report> page=reportMapper.pageQuery(reportPageQueryDTO);
        long total = page.getTotal();
        List<Report> result = page.getResult();
        return new PageResult(total, result);
    }

    //体检报告分页查询-用户端
    @Override
    public PageResult pageQueryUser(ReportPageQueryDTO reportPageQueryDTO) {
        PageHelper.startPage(reportPageQueryDTO.getPage(),reportPageQueryDTO.getPageSize());
        reportPageQueryDTO.setUserId(BaseContext.getCurrentId());
        Page<Report> page=reportMapper.pageQueryUser(reportPageQueryDTO);
        long total = page.getTotal();
        List<Report> result = page.getResult();
        return new PageResult(total, result);
    }




}
